﻿package com.nikitova.craps3d.view 
{
	import jiglib.physics.RigidBody;

	import com.foomonger.utils.Later;
	import com.nikitova.craps3d.constants.Notifications;
	import com.nikitova.craps3d.constants.statemachine.StateActions;
	import com.nikitova.craps3d.constants.statemachine.StateTransitions;
	import com.nikitova.craps3d.model.GameConfigProxy;
	import com.nikitova.craps3d.view.components.game3d.GameView3D;

	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.view.layer.ViewportLayer;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	import org.puremvc.as3.multicore.utilities.startupmanager.model.StartupMonitorProxy;
	import org.puremvc.as3.multicore.utilities.statemachine.StateMachine;

	//	import com.nikitova.craps3d.model.MaterialsProxy;	
			

	/**
	 * @author schranz
	 */
	public class GameView3DMediator extends Mediator 
	{
		static public const NAME : String = "GameView3DMediator";
		private var _gameConfigProxy : GameConfigProxy;

		//private var _materialsProxy  : MaterialsProxy;
		public function GameView3DMediator(gameView3D : GameView3D, mediatorName : String = NAME) 
		{
			super(mediatorName, gameView3D);			
		}

		override public function onRegister() : void 
		{
			component.visible = false;
			_gameConfigProxy = facade.retrieveProxy(GameConfigProxy.NAME) as GameConfigProxy;
		}		

		override public function listNotificationInterests() : Array 
		{		
			return [
						StartupMonitorProxy.LOADING_COMPLETE,
						Notifications.SHOW_GAME,
						StateTransitions.SHOW_BET_STATE,
						Notifications.INIT_GAME_CONFIG
					];
		}

		override public function handleNotification( note : INotification ) : void 
		{			
			switch (note.getName()) 
			{
				case StartupMonitorProxy.LOADING_COMPLETE:
					if(_gameConfigProxy.showAxis)
						component.createAxis();
					component.createStats();
				break;
				case Notifications.SHOW_GAME:
					component.startRendering();
					Later.call(showGame, 10, false);
				break;
				case StateTransitions.SHOW_BET_STATE:	
					
				break;
				case Notifications.INIT_GAME_CONFIG:
					component.setGravity(_gameConfigProxy.gravity);
				break;
			}
		}
		
		public function addBody(body:RigidBody,viewportLayer:ViewportLayer = null):void
		{
		  	component.addBody(body,viewportLayer);
		}
		
		public function addDO3D(do3D:DisplayObject3D,viewportLayer:ViewportLayer = null):void
		{
		  	component.addDO3D(do3D,viewportLayer);
		}
		
		private function showGame():void
		{
			sendNotification(StateMachine.ACTION, null, StateActions.GO_BET_STATE);
			component.visible = true;	
		}
		
		
		public function get camera() : Camera3D 
		{
			return component.cameraAsCamera3D;
		}
		
		public function get component() : GameView3D 
		{
			return viewComponent as GameView3D;
		}
	}
}